﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using FinApp.Model;
using FinApp.Core;
using FinApp.DataProviders.Configuration;
using System.Collections.Generic;
using FinApp.DataProviders.EventArguments;
using FinApp.DataProviders.Enumerations;

namespace FinApp.DataProviders {
    public class CardProvider : Singleton<CardProvider> {

        #region Events
        public event CardCompleteEventHandler CardCompleted;
        #endregion

        #region Members
        private DataContractJsonSerializer serializer;
        private DataContractJsonSerializer serializerCardModel;
        private int indexCardId = 0;
        private List<string> cardIds; /// Su uso es para poder controlar que solo hay una petición a la vez, 
                                      /// ya que WP7 está bloqueado a sólo una petición web a la vez
        #endregion

        #region Constructors
        public CardProvider() {
            this.serializer = new DataContractJsonSerializer(typeof(CardListModel));
            this.serializerCardModel = new DataContractJsonSerializer(typeof(CardModel));
        }
        #endregion

        #region Methods
        public void GetCardStatus() {
            WebClient commerceInfoWebClient = new WebClient();
            commerceInfoWebClient.OpenReadCompleted += new OpenReadCompletedEventHandler(OnOpenReadCompleted);
            commerceInfoWebClient.OpenReadAsync(new Uri(ConfigurationProvider.Instance.GetApiLoggedInUrl() + string.Format("/operations/card/list")));
        }
        #endregion

        #region Events
        private void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {
            WebClient webClient = sender as WebClient;
            webClient.OpenReadCompleted -= new OpenReadCompletedEventHandler(OnOpenReadCompleted);
            CardListModel cardListModel = (CardListModel)serializer.ReadObject(e.Result);
            if (cardListModel.Status == "OK") {
                cardIds = cardListModel.Cards;
                if(indexCardId < cardIds.Count){
                    WebClient cardInfoWebClient = sender as WebClient;
                    cardInfoWebClient.OpenReadCompleted += new OpenReadCompletedEventHandler(OnOpenCardInfoReadCompleted);
                    cardInfoWebClient.OpenReadAsync(new Uri(ConfigurationProvider.Instance.GetApiLoggedInUrl() + string.Format("/operations/card/{0}/status", cardIds[indexCardId++])));
                }
            }
        }

        private void OnOpenCardInfoReadCompleted(object sender,OpenReadCompletedEventArgs e){
            WebClient webClient = sender as WebClient;
            webClient.OpenReadCompleted -= new OpenReadCompletedEventHandler(OnOpenReadCompleted);
            CardModel cardModel = (CardModel)serializerCardModel.ReadObject(e.Result);
            if(this.CardCompleted != null){
                switch (cardModel.Status) {
                    case "OK":
                        this.CardCompleted(this, new CardEventArgs(ProviderResponseEnumeration.OK, string.Empty, cardModel.CardStatusModel));
                        break;
                    default:
                        this.CardCompleted(this, new CardEventArgs(ProviderResponseEnumeration.Failed, cardModel.Msg, null));
                        break;
                }
            }
            if (indexCardId < cardIds.Count) {
                try {
                    WebClient cardInfoWebClient = sender as WebClient;
                    cardInfoWebClient.OpenReadCompleted += new OpenReadCompletedEventHandler(OnOpenCardInfoReadCompleted);
                    cardInfoWebClient.OpenReadAsync(new Uri(ConfigurationProvider.Instance.GetApiLoggedInUrl() + string.Format("/operations/card/{0}/status", cardIds[indexCardId++])));
                } catch {

                }
            }

        }
        #endregion
    }
}
